<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__ADMIN__/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__ADMIN__/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <input type="hidden" id="hideCollectionId" value=""/>
        <input id="isReload" type="hidden" value="no">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit  lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn"> 添加用户 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="auth">权限组</a>
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="__ADMIN__/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="__ADMIN__/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layuimini = layui.layuimini;
        var getUser = function (param = {}) {
            table.render({
                elem: '#currentTableId',
                url: '/admin/user',
                where: param,
                toolbar: '#toolbarDemo',
                parseData: function(res){
                    if(res.code < 0) 
                        return {"msg": res.msg}
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.data.total,
                        "data": res.data.data
                    };
                },
                response: {
                    statusCode: 1 //规定成功的状态码，默认：0
                },
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {type: 'numbers'},
                    {field: 'username', width: 150, title: '账号'},
                    {field: 'nickname', width: 150, title: '用户名'},
                    {field: 'status_text', width: 80, align: 'center', title: '状态',
                        templet: function (d) {
                            let color = '#e01010';
                            if(d.status == 1)
                                color = '#666';
                            return '<span style="color:'+ color +';">'+ d.status_text +'</span>';   
                        }
                    },
                    {field: 'create_ip', width: 150, title: '创建ip'},
                    {field: 'create_time', title: '创建时间', minWidth: 150},
                    {title: '操作', minWidth: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true
            });
        }
        // 显示数据
        getUser();
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (res) {
            const data = res.field;
            let param = {};
            for(let i in data) {
                if(data[i] != '') {
                    param[i] = data[i]
                }
            }
            if(JSON.stringify(param) == "{}") {
                layer.msg('搜索内容不能为空',{icon: 5,time: 500,shade: 0.1});
                return false;
            }
            getUser(param);
            return false;
        });

        // 监听添加操作
        $(document).on("click", ".data-add-btn", function () {
            addPage('添加用户', '/admin/user/add');
            return false;
        });

        // 监控修改删除
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'auth') {
                addPage('添加用户组', '/admin/user/auth', data.id);
                return false;
            } else if (obj.event === 'edit') {
                addPage('编辑用户', '/admin/user/edit', data.id);
                return false;
            } else if (obj.event === 'delete') {
                const title = '您是否要删除：' + data.username + ' 账号？'
                layer.confirm(title, function (index) {
                    $.ajax({
                        url: '/admin/user/del/' + data.id,
                        method: 'delete',
                        success: function (res) {
                            layer.msg(res.msg, {
                                icon: res.code < 0 ? 2 : 1,
                                time: 500,
                                shade: 0.1
                            }, function () {
                                if(res.code > 0)
                                    obj.del();
                                layer.close(index);
                            });
                        }
                    })
                });
            }
        });
        function addPage(title = '', url = '', id = 0)
        {
            $('#hideCollectionId').val(id);
            $('#isReload').val('no');
            var index = layer.open({
                title: title,
                type: 2,
                shade: 0.2,
                shadeClose: true,
                area: ['100%', '100%'],
                content: url,
                end: function() {
                    const isReload = $('#isReload').val();
                        if(isReload == 'yes')
                            table.reload('currentTableId');
                }
            });
        }
    });
</script>
<script>

</script>

</body>
</html>